/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.UserBean;
import com.minierp.dao.AccessoryDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.WorkCenterDao;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.Accessory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class AccessoryFormCtr extends SimpleFormController {
	private AccessoryDao accessoryDao;
	private WorkCenterDao workCenterDao;
	private IdGenerator idGenerator;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		String id = request.getParameter("id");
		if (id == null) {
			id = "";
		}

		refData.put("id", id);
		String agentId = request.getParameter("agent_id");
		if (agentId == null) {
			agentId = "";
		}

		refData.put("agentId", agentId);
		String code = request.getParameter("code");
		if (code == null) {
			code = "";
		}

		refData.put("code", code);
		String type = request.getParameter("type");
		if (type == null) {
			type = "";
		}

		refData.put("type", type);
		if (type.equals("SHOW_EXT")) {
			refData.put("wcList", this.workCenterDao.getList(true));
		}

		String agentType = request.getParameter("agent_type");
		refData.put("agentType", agentType);
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String id = request.getParameter("id");
		return id != null && !id.equals("") ? this.accessoryDao.get(
				Accessory.class, Long.parseLong(id)) : new Accessory();
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		ModelAndView mv = new ModelAndView(this.getSuccessView());
		Accessory obj = (Accessory) command;
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		String id = request.getParameter("id");
		if (id != null && !id.equals("")) {
			if ((obj.getFileName() == null || obj.getFileName().equals(""))
					&& obj.getCustomPath() != null
					&& !obj.getCustomPath().trim().equals("")) {
				obj.setCustomPath(obj.getCustomPath().trim());
				obj.setSourceName(this.getPurFileName(obj.getCustomPath()));
			}

			if (obj.getSourceName() == null || obj.getSourceName().equals("")) {
				errors.rejectValue("customPath", "", "请输入资料路径");
				return this.showForm(request, errors, this.getFormView());
			}

			if (obj.getAccType() == null || !obj.getAccType().equals("DESIGN")) {
				obj.setExtId(0L);
			}

			this.accessoryDao.update(obj);
		} else {
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			MultipartFile multipartFile = multipartRequest
					.getFile("accessory_file");
			String sourceFileName = request.getParameter("hid_file_name");
			String agentType;
			String code;
			if (multipartFile != null && !multipartFile.isEmpty()
					&& sourceFileName != null && !sourceFileName.equals("")) {
				sourceFileName = sourceFileName.replace("/", "\\");
				String[] agentId = sourceFileName.split("\\\\");
				agentType = agentId[agentId.length - 1];
				code = EncCommonHelper.getUploadVirtualPath(this
						.getServletContext());
				String realPath = this.getServletContext().getRealPath(code);
				File f = new File(realPath
						+ System.getProperty("file.separator") + agentType);
				int lastIndex = agentType.lastIndexOf(".");
				String partName = agentType;
				String extendName = "";
				if (lastIndex != -1) {
					extendName = agentType.substring(lastIndex + 1);
					partName = agentType.substring(0, lastIndex);
				}

				for (int index = 0; f.exists() && f.isFile(); f = new File(
						realPath + System.getProperty("file.separator")
								+ agentType)) {
					++index;
					agentType = partName + "(" + index + ")." + extendName;
				}

				InputStream in = null;
				FileOutputStream out = null;

				try {
					in = multipartFile.getInputStream();
					out = new FileOutputStream(new File(realPath
							+ System.getProperty("file.separator") + agentType));

					byte[] b;
					for (boolean flag = true; flag; b = (byte[]) null) {
						b = new byte[1024];
						int num = in.read(b);
						if (num > -1) {
							out.write(b);
						} else {
							flag = false;
						}
					}
				} finally {
					try {
						if (in != null) {
							in.close();
						}
					} catch (Exception arg33) {
						;
					}

					try {
						if (out != null) {
							out.close();
						}
					} catch (Exception arg32) {
						;
					}

				}

				obj.setSourceName(agentType);
				obj.setFileName(code + agentType);
			} else if (obj.getCustomPath() != null
					&& !obj.getCustomPath().trim().equals("")) {
				obj.setCustomPath(obj.getCustomPath().trim());
				obj.setSourceName(this.getPurFileName(obj.getCustomPath()));
			}

			if (obj.getSourceName() == null || obj.getSourceName().equals("")) {
				errors.rejectValue("fileName", "", "请选择要上传的文件，或输入资料路径");
				return this.showForm(request, errors, this.getFormView());
			}

			String arg35 = request.getParameter("agent_id");
			agentType = request.getParameter("agent_type");
			if (arg35 != null && !arg35.equals("")) {
				obj.setAgentId(Long.parseLong(arg35));
			} else {
				code = request.getParameter("code");
				obj.setTmpCode(code);
			}

			obj.setAgentType(agentType);
			if (ub.getStaffer() != null) {
				obj.setCreaterId(ub.getStaffer().getId());
				obj.setCreaterName(ub.getStaffer().getStafferName());
			}

			obj.setCreateTime(new Date());
			obj.setId(this.idGenerator.getId(Accessory.class.getName()));
			this.accessoryDao.save(obj);
		}

		mv.addObject("id", String.valueOf(obj.getId()));
		mv.addObject("fileName", obj.getFileName());
		return mv;
	}

	private String getPurFileName(String str) {
		String sourceName = "";
		if (str != null) {
			sourceName = str.trim().replace("/", "\\");
			if (sourceName.endsWith("\\")) {
				sourceName = sourceName.substring(0, sourceName.length() - 1);
			}

			int lastIndex = sourceName.lastIndexOf("\\");
			if (lastIndex != -1) {
				sourceName = sourceName.substring(lastIndex + 1);
			}
		}

		return sourceName;
	}

	public AccessoryDao getAccessoryDao() {
		return this.accessoryDao;
	}

	public void setAccessoryDao(AccessoryDao accessoryDao) {
		this.accessoryDao = accessoryDao;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public WorkCenterDao getWorkCenterDao() {
		return this.workCenterDao;
	}

	public void setWorkCenterDao(WorkCenterDao workCenterDao) {
		this.workCenterDao = workCenterDao;
	}
}